/*
 * 类名称:ProductRelateShopServiceImpl.java
 * 包名称:com.platform.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2019-08-28 14:42:41        lipengjun     初版做成
 *
 * Copyright (c) 2019-2019 微同科技
 */
package com.platform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.dao.ProductRelateShopDao;
import com.platform.dao.RegionRelateBedroomDao;
import com.platform.entity.ProductEntity;
import com.platform.entity.ProductRelateShopEntity;
import com.platform.entity.RegionRelateBedroomEntity;
import com.platform.service.ProductRelateShopService;
import com.platform.service.ProductService;
import com.platform.utils.Query;
import com.platform.utils.RRException;
import com.platform.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.REException;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商铺产品关系表Service实现类
 *
 * @author lipengjun
 * @date 2019-08-28 14:42:41
 */
@Service("productRelateShopService")
public class ProductRelateShopServiceImpl extends ServiceImpl<ProductRelateShopDao, ProductRelateShopEntity>  implements ProductRelateShopService {

    @Autowired
    private ProductRelateShopDao productRelateShopDao;
    @Autowired
    private ProductService productService;
    @Override
    public List<ProductRelateShopEntity> queryAll(Map<String, Object> params) {
        return productRelateShopDao.queryAll(params);
    }


    @Override
    public int add(ProductRelateShopEntity productRelateShop) {
        if(productRelateShop!=null) {
            ProductRelateShopEntity productRelateShopEntity = productRelateShopDao.queryByProductId(productRelateShop.getProductId(), productRelateShop.getShopId());
            if (productRelateShopEntity != null) {
                Integer stockNum = productRelateShopEntity.getStockNum();
                Integer stockNum1 = productRelateShop.getStockNum();
                productRelateShopEntity.setStockNum(stockNum + stockNum1);
                return productRelateShopDao.updateById(productRelateShopEntity);

            } else {
                Long userId = ShiroUtils.getUserId();
                productRelateShop.setCreateTime(new Date());
                productRelateShop.setCreateUserId(userId);
                ProductEntity productEntity = productService.queryObject(productRelateShop.getProductId());
                if (productEntity != null) {
                    productRelateShop.setSalePrice(productEntity.getRetailPrice());
                } else {
                    throw new RRException("商品上架信息异常！");
                }
                return productRelateShopDao.insert(productRelateShop);
            }
        }else{
            throw new RRException("商品上架信息异常！");
        }
    }

    @Override
    public int update(ProductRelateShopEntity productRelateShop) {
        if(productRelateShop!=null){
            Long userId = ShiroUtils.getUserId();
            productRelateShop.setUpdateTime(new Date());
            productRelateShop.setUpdateUserId(userId);
        }
        return productRelateShopDao.updateById(productRelateShop);
    }

    @Override
    public int delete(Integer id) {
        return productRelateShopDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)

    public int deleteBatch(Integer[] ids) {
        return productRelateShopDao.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<ProductRelateShopEntity> queryList(Query query) {
        return productRelateShopDao.queryList(query);
    }

    @Override
    public int queryTotal(Query query) {
        return productRelateShopDao.queryTotal(query);
    }

    @Override
    public ProductRelateShopEntity selectById(Integer id) {
        return productRelateShopDao.getOneById(id);
    }


}
